package com.unsiv.blplayer.playtools;

import java.io.File;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.unsiv.blplayer.util.MyDBHelper;
import com.unsiv.blplayer.util.MyFileHelper;

/**
 * 
 * @author fanzhen
 * @time 10:40:20 PM Oct 4, 2011
 * @email fanzhen.meng@gmail.com
 */
public final class MP3Logic {
	public static Context context;
	private static final String TAG = "MP3Logic";

	public static void MP3Work(Integer oidCode) {
		MP3Player.oidCode = oidCode;
		MP3Player.playType = "MP3";
		if (MyFileHelper.getFileCount(MyFileHelper.G_Mp3Folder,".mp3") > 100) // 如果Mp3文件超出100 默认删除第一个
		{
			MP3Player.deleteMp3Case();
		}
		switch (oidCode) {
		case 1000201: // 开始播放-从第一首
			MP3Player.init();
			MP3Player.playMp3();
			break;
		case 1000202: // 播放和暂停
			MP3Player.pause();
			break;
		case 1000203: // 上一曲
			MP3Player.playPre();
			break;
		case 1000204: // 下一曲
			MP3Player.playNext();
			break;
		case 1000205: // 停止播放
			MP3Player.stop();
			break;
		case 1000206: // 存放箱/热点删除
			MP3Player.hasMP3Delete = true;
			break;
		case 1000207: // 存入热点
			String Mp3FileName = MP3Player.fileName;// 获取文件目录//System.IO.Path.GetFileName(MP3Player.FileName);
			MP3Player.pause();
			if (Mp3FileName != null && !Mp3FileName.equals("")) {
				saveToHot(Mp3FileName);
			}
			MP3Player.pause();
			break;
		case 1000208: // 单播/连续状态切换
			if (MP3Player.getStatus() == "") {
				return;
			}
			MP3Player.isLinkPlay = !MP3Player.isLinkPlay;
			break;
		case 1000209: // 退出(删除/存入状态)
			break;
		case 1000210: // 0+
			MP3Player.mpSectionBase = 0;
			break;
		case 1000211: // 25+
			MP3Player.mpSectionBase = 25;
			break;
		case 1000212: // 50+
			MP3Player.mpSectionBase = 50;
			break;
		case 1000213: // 75+
			MP3Player.mpSectionBase = 75;
			break;
		case 1000233: // 增加音量
			MP3Player.volumeAll += 100;
			break;
		case 1000234: // 降低音量
			MP3Player.volumeAll -= 100;
			break;
		}
		// 播放Mp3存放区
		if (oidCode >= 1000401 && oidCode <= 1000450) // 1000401表示第一首歌
		{
			Log.i(TAG, "MP3Player.Status:" + "1000401");

			if ((MP3Player.oidCode == MP3Player.lastOidCode)
					&& MP3Player.getStatus() == "playing") {
				Log.i(TAG, "MP3Player.Status:" + MP3Player.getStatus());
				return;
			}
			MP3Player.mp3Index = (oidCode - 1000401 + 1);
			if (MP3Player.mp3Index > MP3Player.getFileCount()) // 如果点击的歌曲不存在 暂停
			{
				MP3Player.stop();
				return;
			}
			if (MP3Player.hasMP3Delete) // 如果有删除标志
			{
				MP3Player.close();
				MP3Player.deleteMp3Case(); // 删除暂存箱
				int oid = (int) (oidCode - 1000401);
				Cursor cursor = MyDBHelper.getMyDBHelper(context)
						.getHotDataByOid(oid);
				if (cursor.getCount() > 0) {
					cursor.moveToFirst();
					String name = cursor.getString(2);
					File file = new File(MyFileHelper.G_Mp3Folder + name);
					if (null != file && file.exists()) // 如果热点区存在此文件就删除
					{
						MyDBHelper.getMyDBHelper(context).delHotData(name); // 删除热点
					}
					cursor.close();
				}
				MP3Player.hasMP3Delete = false;
			} else {
				MP3Player.playMp3();
			}
		}
		// //MP3热点区
		if (oidCode >= 1000601 && oidCode <= 1000620) {
			String name = null;
			if (MP3Player.getStatus() == "palying") {
				MP3Player.stop();
			}
			int oid = (int) (oidCode - 1000601);
			Cursor cursor = MyDBHelper.getMyDBHelper(context).getHotDataByOid(
					oid);

			if (oid < cursor.getCount()) // 如果播放的歌曲存在
			{
				if (cursor.getCount() > 0) {
					cursor.moveToFirst();
					name = cursor.getString(2);
				}
			}
			if (new File(MyFileHelper.G_Mp3Folder + name).exists()) {
				MP3Player.bTriggerEvent = false;
				MP3Player.playMp3(name);
			}
			return;
		}
	}

	// / <summary>
	// / MP3 存入热点
	// / </summary>
	// / <param name="Mp3FileName"></param>
	private static void saveToHot(String mp3FileName) {
		if (MyDBHelper.getMyDBHelper(context).hasHotData(mp3FileName)) // 如果歌曲已存在
																		// 删除原来的MP3
																		// 然后添加
		{
			MyDBHelper.getMyDBHelper(context).delHotData(mp3FileName);
			MyDBHelper.getMyDBHelper(context).inesrtHotData(
					MP3Player.mpSectionBase + MP3Player.mp3Index, mp3FileName);
		} else // 直接添加
		{
			MyDBHelper.getMyDBHelper(context).inesrtHotData(
					MP3Player.mpSectionBase + MP3Player.mp3Index, mp3FileName);
		}
	}
}